package com.victor.controller.app;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.swing.plaf.basic.BasicInternalFrameTitlePane.IconifyAction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alipay.api.domain.OrderDetail;
import com.victor.entity.Cart;
import com.victor.entity.JingqubusinessShop;
import com.victor.entity.JingqubusinessOrder;
import com.victor.service.GoodsService;
import com.victor.utils.Msg;

/**
 * app端商品
 * @author qinyuquan
 *
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {
	
	@Autowired
	private GoodsService goodsService;

	
	/**
	 * 搜索框模糊查询商品列表
	 * search （空字符串查询所有）
	 */
	@RequestMapping("/search")
	public Msg search(String search) {
		if (search==null) {
			return Msg.fail("参数错误");
		}
		List<JingqubusinessShop> goodsList = goodsService.search(search);
		for (int i = 0; i < goodsList.size(); i++) {
			int sum = goodsService.getSum(goodsList.get(i).getId()+"");
			goodsList.get(i).setSellcount(sum+"");
		}
		return Msg.success().add("goodsList", goodsList);
	}
	
	/**
	 * 	获取商品详情
	 * 	id 商品id
	 */
	@RequestMapping("/goodsDetail")
	public Msg goodsDetail(Integer id) {
		if (id==null) {
			return Msg.fail("参数错误");
		}
		JingqubusinessShop goodsDetail = goodsService.goodsDetail(id);
		//获取该商品的销售量
		int sum = goodsService.getSum(id+"");
		goodsDetail.setSellcount(sum+"");
		//获取该商品的收藏量
		int favorites = goodsService.getFavorites(id+"");
		goodsDetail.setFavorites(favorites+"");
		return Msg.success().add("goodsDetail", goodsDetail);
	}
	
	/**
	 * 	先根据搜索框搜索商品
	 * 	再根据平台type查询商品列表 ：平台自营，个体自营，景区自营等
	 */
	@RequestMapping("/selByType")
	public Msg selByType(String search,String type) {
		if (search==null || type==null || type.equals("")) {
			return Msg.fail("参数错误");
		}
		List<JingqubusinessShop> goodsList = goodsService.selByType(search,type);
		return Msg.success().add("goodsList", goodsList);
	}
	
	/**
	 * 	加入购物车
	 */
	@RequestMapping("/addCart")
	public Msg addShopcart(Cart cart) {
		if (cart==null || cart.getShopid()==null || cart.getStoreid() ==null || cart.getPhoto()==null || cart.getStore()==null || cart.getStoreid()==null || cart.getDanjia()==null || cart.getParameter()==null || cart.getUserid()==null) {
			return Msg.fail("参数错误");
		}
		boolean b = goodsService.addShopcart(cart);
		return Msg.judge(b);
	}
	
	/**
	 * 	通过当前登录用户id，
	 * 	查看购物车列表
	 */
	@RequestMapping("/selShopcart")
	public Msg selShopcart(String userid) {
		if (userid==null || userid.equals("")) {
			return Msg.fail("参数错误");
		}
		List<Cart> shopCartsList = goodsService.selShopcart(userid);
		return Msg.success().add("shopCartsList", shopCartsList);
	}

	/**
	 * 	根据 每个购物车id
	 * 	更改购物车中，商品的数量
	 */
	@RequestMapping("/updateCart")
	public Msg updateCart(Cart cart) {
		if (cart ==null || cart.getId()==null || cart.getCount()==null || cart.getUserid()==null) {
			return Msg.fail("参数错误");
		}
		boolean b = goodsService.updateCart(cart);
		return Msg.judge(b);
	}

	/**
	 * 	批量删除购物车
	 */
	@RequestMapping("/batchDelete")
	public Msg batchDelete(int[] ids) {
		if (ids==null ||ids.length==0) {
			return Msg.fail("参数错误");
		}
		boolean b = goodsService.batchDelete(ids);
		return Msg.judge(b);
	}
	
}
